﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Identity;
using Microsoft.EntityFrameworkCore;
using Ornament.ImportHelper;

namespace Fee.IO.Importers
{
    public class StationImporter<T> : BaseImporter<T> where T : FeeDbContext
    {
        private readonly IDictionary<string, District> Mapping;

        public StationImporter(T context) : base(context, true)
        {
            Mapping = context.Districts.ToDictionary(f => f.Name, f => f);
        }

        protected override bool ConverTo(IList<string> rowStringInfo, out ImportResultItem item)
        {
            //   名称 站点服务区域  是否重客 是否建于2017年前
            item = new ImportResultItem();
            var index = 0;
            var stationName = rowStringInfo.ImportString(index++, "名称", item, true);
            var stationCode = rowStringInfo.ImportString(index++, "编码", item, true);
            var stationType = rowStringInfo.ImportEnum<StationType>(index++, "站点类型", item, true);

            var districtNameOrder = rowStringInfo.ImportString(index++, "站点提单区域", item, true);
            var districtName4Servere = rowStringInfo.ImportString(index++, "站点服务区域", item, true);

            var thirdStatus = rowStringInfo.ImportString(index++, "状态", item);
            var number = rowStringInfo.ImportInt(index++, "户数", item, false);

            var isVip = rowStringInfo.ImportString(index++, "是否重客", item, false) == "是";
            var before2017 = rowStringInfo.ImportString(index++, "是否建于2017年", item, false) == "是";

            if (item.HasError) return false;

            if (!Mapping.TryGetValue(districtName4Servere, out var district4Service))
            {
                item.AddError(1, "'" + districtName4Servere + "'并不存在");
                return false;
            }


            if (!Mapping.TryGetValue(districtNameOrder, out var district4Order))
                item.AddError(1, "'" + districtName4Servere + "'并不存在");

            if (item.HasError)
                return false;


            var station =
                          new Station { Code = stationCode };

            //station.Status = StationStatus.S1;
            station.Name = stationName;
            station.District = district4Service;
            station.OrderDistrict = district4Order;
            station.CreateTime = before2017 ? Station.Date2017 : DateTimeOffset.Now;
            station.IsVip = isVip;
            station.Type = stationType;
            station.UsersNumber = number;
            station.ThirdStationStatus = thirdStatus;
            item.Data.Add(station);
            return true;
        }

        public override void SaveToDb(IList<ImportResultItem> importResult, T t)
        {
          
            var codeSet = new List<Task>();
            foreach (var importResultItem in importResult)
            {
                var station = importResultItem.Data[0] as Station;

                Task taskAsync = Context.Stations.FirstOrDefaultAsync(f => f.Code == station.Code)
                    .ContinueWith(task =>
                    {
                        var stationDb = task.Result;
                        if (stationDb == null)
                        {
                            Context.Add(station);
                        }
                        else
                        {
                            stationDb.Name = station.Name;
                            stationDb.District = station.District;
                            stationDb.OrderDistrict = station.OrderDistrict;
                            stationDb.CreateTime = station.CreateTime;
                            stationDb.IsVip = station.IsVip;
                            stationDb.Type = station.Type;
                            stationDb.UsersNumber = station.UsersNumber;
                            stationDb.ThirdStationStatus = station.ThirdStationStatus;
                            Context.Update(stationDb);
                        }
                    });

                codeSet.Add(taskAsync);

                Task.WhenAll(codeSet).Wait();
                Context.SaveChanges();

            }



        }
    }
}